# Logistic Regression
# 绘制ROC曲线

# import lib
import numpy as np
import matplotlib.pyplot as plt

# def loaddata
def loaddata(filename):
    file = open(filename)
    x=[]
    y=[]
    for line in file.readlines():
        line = line.strip().split()
        x.append([1,float(line[0]),float(line[1])])
        y.append(float(line[-1]))
    xmat = np.mat(x)
    ymat = np.mat(y).T
    file.close()
    return xmat, ymat

score, y= loaddata('E:\Microsoft VS Code\VSCode\machine_learning\LR\dataSet.txt')
score = np.array(score)
y = np.array(y)

# false positive rate
fpr = []

# true positive rate
tpr = []

# Iterate thresholds from 0.0, 0.01, ... 1.0
thresholds = np.arange(0.0, 1.01, .01)

# get number of positive and negative examples in the dataset
P = sum(y)
N = len(y) - P

# iterate through all thresholds and determine fraction of true positives
# and false positives found at this threshold
for thresh in thresholds:
    FP=0
    TP=0
    for i in range(len(score)):
        if (score[i,1] > thresh):
            if y[i] == 1:
                TP = TP + 1
            if y[i] == 0:
                FP = FP + 1
        fpr.append(FP/float(N))
        tpr.append(TP/float(P))
    plt.scatter(fpr, tpr,c='r')

plt.show()